<template>
  <div class="pay-order">
    <div class="timer">
      <div class="img">
        <img src="@/assets/img/clock.png" />
      </div>
      <div class="time" v-if="maxtime > 0">
        剩余支付时间
        <br />
        {{ time }}
      </div>
      <div class="time" v-else>
        已超时，订单取消
        <br />
        无法支付
      </div>

      <div class="warn">
        逾期订单自动取消
        <br />
        请尽快支付
      </div>
    </div>
    <div class="right">
      <div class="pay-way">
        <div class="choice">
          <img
            class="button-img ali"
            src="@/assets/img/aliPay.png"
            @click="ali"
          />
          <br />
          <img
            v-if="aliPay"
            class="code"
            src="@/assets/img/aliPayCode.png"
            alt="二维码无法显示"
          />
        </div>
        <div class="separator"></div>
        <div class="choice">
          <img
            class="button-img wechat"
            src="@/assets/img/wechatPay.png"
            @click="wechat"
          />
          <br />
          <img
            v-if="wechatPay"
            class="code"
            src="@/assets/img/wechatPayCode.png"
            alt="二维码无法显示"
          />
        </div>
      </div>
      <div class="pay-bottom">
        <div class="money">
          订单总金额：
          <span>￥{{ money }}</span>
        </div>
        <input
          type="button"
          :class="{ overtime: maxtime === 0 }"
          value="确认支付"
          @click="submit"
        />
      </div>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref, onMounted } from "vue";
import { Order } from "@/utils/api/order";
import { ElMessage } from 'element-plus';

export default defineComponent({
  props: {
    money: Number,
		order: Object
  },
  setup(props, context) {
    //计时器
    const time = ref("");
    var maxtime = ref(15 * 60);
    const timer = () => {
      let minutes = Math.floor(maxtime.value / 60);
      let seconds = Math.floor(maxtime.value % 60);
      time.value = minutes + "分" + seconds + "秒";
      if (maxtime.value > 0) maxtime.value -= 1;
    };
    onMounted(() => {
      setInterval(timer, 1000);
    });
    //支付方式
    const aliPay = ref(true);
    const wechatPay = ref(false);
    const wechat = () => {
      wechatPay.value = true;
      aliPay.value = false;
    };
    const ali = () => {
      wechatPay.value = false;
      aliPay.value = true;
    };
    //提交表单
    const submit = () => {
      if (maxtime.value == 0) {
        openError('订单已经超时，无法支付！');
      } else {
				Order.sendOrder(props.order as any)
				.then((res) => {
					console.log(res.data);
          if(res.data.msg === '订单重复！') {
            openError('订单重复！')
          } else if(res.data.msg === '预定成功!') {
            context.emit('next2');
          } else if(res.data.msg === '信用太低，没有权限！') {
            openError('信用太低，无法购票');
          } else {
            openError('出错了，请联系管理员！');
          }
					
				})
				.catch((error) => {
					console.log(error)
				});
			}	
		};
    const openError = (msg: string) => {
      ElMessage({
            showClose: true,
            message: msg,
            type: 'error',
            offset: 200,
          });
    }
    return {
      time,
      maxtime,
      aliPay,
      wechatPay,
      wechat,
      ali,
      submit,
    };
	}	
});
</script>
<style lang="scss" scoped>
@import "@/assets/css/config.scss";
.timer {
  display: inline-block;
  height: 462px;
  width: 200px;
  background: rgb(251, 251, 251);
  border: 1px solid $themeLightGray;
  padding: 10px;
  div.img {
    display: inline-block;
    img {
      width: 40px;
      height: 40px;
      padding: 5px;
    }
  }
  div.time {
    margin-left: 10px;
    margin-top: 3px;
    display: inline-block;
    vertical-align: top;
    text-align: center;
  }
  .warn {
    color: rgb(100, 164, 228);
    margin: 15px;
    padding: 20px 15px;
    text-align: center;
  }
}
div.right {
  display: inline-block;
  vertical-align: top;
  text-align: center;
}
.pay-way {
  padding: 20px;
  text-align: center;
  vertical-align: top;
  height: 265px;
  border: 1px solid $themeLightGray;
  div.choice {
    display: inline-block;
    vertical-align: top;
    padding: 10px;
    img.button-img {
      cursor: pointer;
      width: 200px;
      height: 50px;
      border: 1px solid $themeGray;
      border-radius: 4px;
    }
    img.ali:hover {
      border-color: rgb(24, 118, 254);
    }
    img.wechat:hover {
      border-color: rgb(29, 170, 57);
    }

    .code {
      margin-top: 10px;
      height: 170px;
      width: 170px;
    }
  }
  .separator {
    width: 50px;
    height: 10px;
    display: inline-block;
  }
}
div.pay-bottom {
  padding-top: 40px;
  border: 1px solid $themeLightGray;
  div.money {
    span {
      color: rgb(255, 102, 0);
    }
  }
  input[type="button"] {
    cursor: pointer;
    background: rgb(255, 102, 0);
    color: white;
    padding: 15px 0;
    letter-spacing: 8px;
    text-indent: 8px;
    width: 200px;
    margin-top: 30px;
    margin-bottom: 30px;
    font-size: 18px;
    border: none;
    outline: none;
    border-radius: 6px;
  }
  input.overtime {
    background: rgb(102, 102, 102);
  }
}
</style>
